#!/bin/bash
#跳板机推送

error_exit(){
	echo "ERROR: 跳板机推送失败,$1"
	echo "ERROR: 跳板机推送失败,$1" >> ${logFile}
    rm -rf ${projectPath}/${editionNum}/push_${jumpServerIp}.lock
	exit 1
}


rootPath=$1
projectName=$2
jumpServerIp=$3
recordName=$4
editionNum=$5
ssh_login_user=$6
ssh_key_file=$7
ssh_port=$8
shellPath=$9

projectPath=${10}

logFile="${rootPath}/Beetle/pushRecord/${recordName}/push.log"

loginUser=`whoami`
ssh_key_file=/home/${loginUser}/${ssh_key_file}

echo "跳板机推送开始,projectName=${projectName},jumpServerIp=${jumpServerIp}" >> ${logFile}

if [ ! ${projectPath} ]
then
    error_exit "参数存在空值projectPath=${projectPath}"
fi
if [ ! ${editionNum} ]
then
    error_exit "参数存在空值editionNum=${editionNum}"
fi

cd ${projectPath}/${editionNum} >> ${logFile} || error_exit "进入版本目录失败.${projectPath}/${editionNum}"

#校验远程终端是否可以免密登录
expect ${shellPath}/shell/init/beetle_validate_ssh.sh ${jumpServerIp} ${ssh_login_user} ${ssh_login_user} ${ssh_port} > /dev/null || error_exit "校验ssh免密登录失败.${jumpServerIp}"

ssh ${ssh_login_user}@${jumpServerIp} -p ${ssh_port} -i ${ssh_key_file} "rm -rf ${projectPath}/${editionNum}" >> ${logFile} || error_exit "删除跳板机旧版本失败.${jumpServerIp}"

ssh ${ssh_login_user}@${jumpServerIp} -p ${ssh_port} -i ${ssh_key_file} "mkdir -p ${projectPath}/${editionNum}" >> ${logFile} || error_exit "创建版本目录失败.${jumpServerIp}"

ssh ${ssh_login_user}@${jumpServerIp} -p ${ssh_port} -i ${ssh_key_file} "mkdir -p ${projectPath}/${editionNum}/logs" >> ${logFile} || error_exit "创建版本日志目录失败.${jumpServerIp}"

script -a ${logFile} -q -c "scp -v -P ${ssh_port} -i ${ssh_key_file} ${projectPath}/${editionNum}/push.tgz ${ssh_login_user}@${jumpServerIp}:${projectPath}/${editionNum}/" >> ${logFile} || error_exit "远程拷贝war包失败.${jumpServerIp}"

ssh ${ssh_login_user}@${jumpServerIp} -p ${ssh_port} -i ${ssh_key_file} "cd ${projectPath}/${editionNum};tar -zxf push.tgz" >> ${logFile} || error_exit "解压推送包失败.${jumpServerIp}"

echo "跳板机推送结束,projectName=${projectName},jumpServerIp=${jumpServerIp}" >> ${logFile}
echo "success"